package com.t2t.top;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/9.
 */
public class RedisCs {

    private ShardedJedisPool shardedJedisPool;

    private ShardedJedis shardedJedis;

    @Resource(name = "jedisConnectionFactory")
    private JedisConnectionFactory jedisConnectionFactory;

    /**
     * 初始化切片池
     */
    private void initialShardedPool() {
        // slave链接
        List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
        shards.add(new JedisShardInfo(jedisConnectionFactory.getHostName(), jedisConnectionFactory.getPort(), "master"));
        shardedJedisPool = new ShardedJedisPool(jedisConnectionFactory.getPoolConfig(), shards);
        shardedJedis = shardedJedisPool.getResource();
    }

    public String set(String key, String value) {
        String result = shardedJedis.set(key, value);
        shardedJedisPool.returnResource(shardedJedis);
        return result;
    }
}
